package com.castle.fortress.admin.member.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.admin.utils.BizCommonUtil;
import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.member.entity.MemberCollectEntity;
import com.castle.fortress.admin.member.dto.MemberCollectDto;
import com.castle.fortress.admin.member.mapper.MemberCollectMapper;
import com.castle.fortress.admin.member.service.MemberCollectService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;
import java.util.Map;

/**
 * 会员收藏表 服务实现类
 *
 * @author castle
 * @since 2023-02-14
 */
@Service
public class MemberCollectServiceImpl extends ServiceImpl<MemberCollectMapper, MemberCollectEntity> implements MemberCollectService {
    /**
    * 获取查询条件
    * @param memberCollectDto
    * @return
    */
    public QueryWrapper<MemberCollectEntity> getWrapper(MemberCollectDto memberCollectDto){
        QueryWrapper<MemberCollectEntity> wrapper= new QueryWrapper();
        if(memberCollectDto != null){
            MemberCollectEntity studyCollectEntity = ConvertUtil.transformObj(memberCollectDto, MemberCollectEntity.class);
            wrapper.like(studyCollectEntity.getId() != null,"id",studyCollectEntity.getId());
            wrapper.like(StrUtil.isNotEmpty(studyCollectEntity.getCollectType()),"collect_type",studyCollectEntity.getCollectType());
            wrapper.like(studyCollectEntity.getMemberId() != null,"member_id",studyCollectEntity.getMemberId());
            wrapper.like(studyCollectEntity.getTargetId() != null,"target_id",studyCollectEntity.getTargetId());
            wrapper.like(studyCollectEntity.getCollectTime() != null,"collect_time",studyCollectEntity.getCollectTime());
        }
        return wrapper;
    }


    @Override
    public IPage<MemberCollectDto> pageStudyCollect(Page<MemberCollectDto> page, MemberCollectDto memberCollectDto) {
        QueryWrapper<MemberCollectEntity> wrapper = getWrapper(memberCollectDto);
        Page<MemberCollectEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<MemberCollectEntity> studyCollectPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<MemberCollectDto> pageDto = new Page(studyCollectPage.getCurrent(), studyCollectPage.getSize(),studyCollectPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(studyCollectPage.getRecords(), MemberCollectDto.class));
        return pageDto;
    }

    @Override
    public IPage<MemberCollectDto> pageStudyCollectExtends(Page<MemberCollectDto> page, MemberCollectDto memberCollectDto) {
        Map<String,Long> pageMap = BizCommonUtil.getPageParam(page);
        MemberCollectEntity entity = ConvertUtil.transformObj(memberCollectDto,MemberCollectEntity.class);
        List<MemberCollectEntity> list=baseMapper.extendsList(pageMap,entity);
        Long total = baseMapper.extendsCount(entity);
        Page<MemberCollectDto> pageDto = new Page(page.getCurrent(), page.getSize(),total);
        pageDto.setRecords(ConvertUtil.transformObjList(list,MemberCollectDto.class));
        return pageDto;
    }


    @Override
    public List<MemberCollectDto> listStudyCollect(MemberCollectDto memberCollectDto){
        QueryWrapper<MemberCollectEntity> wrapper = getWrapper(memberCollectDto);
        List<MemberCollectEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list, MemberCollectDto.class);
    }

    @Override
    public MemberCollectEntity getByIdExtends(Long id) {
        return baseMapper.getByIdExtends(id);
    }
}

